package com.jxl.application.controller.controller;

import com.jxl.application.controller.convert.AuthPermissionDtoConvert;
import com.jxl.dto.AuthPermissionDto;
import com.jxl.dto.Result;
import com.jxl.domain.entity.AuthPermissionBO;
import com.jxl.domain.service.AuthPermissionDomainService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限模块
 */
@RestController
@RequestMapping("/permission/")
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 权限添加
     * @param authPermissionDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDto authPermissionDto){
        try{
            AuthPermissionBO authPermissionBO =
                    AuthPermissionDtoConvert.INSTANCE.convertDtoToAuthPermissionBO(authPermissionDto);
            Boolean add = authPermissionDomainService.add(authPermissionBO);
            return Result.ok(add);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 权限修改
     * @param authPermissionDto
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody  AuthPermissionDto authPermissionDto){
        try{
            AuthPermissionBO authPermissionBO =
                    AuthPermissionDtoConvert.INSTANCE.convertDtoToAuthPermissionBO(authPermissionDto);
            Boolean add = authPermissionDomainService.update(authPermissionBO);
            return Result.ok(add);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 权限删除
     * @param authPermissionDto
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody  AuthPermissionDto authPermissionDto){
        try{
            AuthPermissionBO authPermissionBO =
                    AuthPermissionDtoConvert.INSTANCE.convertDtoToAuthPermissionBO(authPermissionDto);
            Boolean add = authPermissionDomainService.delete(authPermissionBO);
            return Result.ok(add);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 权限删除
     * @param userName
     * @return
     */
    @GetMapping("/getPermission")
    public Result<Boolean> delete(String  userName){
        try{
            List<String> permission = authPermissionDomainService.getPermission(userName);
            return Result.ok(permission);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }
}
